
-- $Id: FLer_Core.lua 41 2009-03-25 03:52:41Z Fylgjas $

-- ========================================================================= --
-- Copyright © 2009 Thomas ROBERT                                            --
-- ========================================================================= --
-- This file is part of FyLooter.                                            --
--                                                                           --
-- FyLooter is free software: you can redistribute it and/or modify it under --
-- the terms of the GNU General Public License as published by the Free      --
-- Software Foundation, either version 3 of the License, or (at your option) --
-- any later version.                                                        --
--                                                                           --
-- FyLooter is distributed in the hope that it will be useful, but WITHOUT   --
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     --
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for  --
-- more details.                                                             --
--                                                                           --
-- You should have received a copy of the GNU General Public License along   --
-- with FyLooter. If not, see <http://www.gnu.org/licenses/>.                --
-- ========================================================================= --
  
FyLooter.Core.uncommitedChanges = false;

-- ========================================================================= --

function FyLooter.Core:OnMasterLootListOpened()
  FyLooter:Debug("OnMasterLootListOpened()");

  local loot = {};

  loot.slot = LootFrame.selectedSlot;
  loot.item = GetLootSlotLink(loot.slot);
  loot.ml = UnitName("player");

  local quality = select(3, GetItemInfo(loot.item));

  local threshold = GetLootThreshold();

  if (not threshold) then threshold = 4 end

  if (quality < threshold) then return end

  FyLooter.Gui.MinimapButton:Disable();
  FyLooter.Gui.MainFrame:Hide();

  local candidates = {};

  for i = 1, 40 do
    local candidate = GetMasterLootCandidate(i);

    if (candidate) then
      local playerIdx, characterIdx = FyLooter.Model:GetCharacterIdx(candidate);

      if (playerIdx and characterIdx) then    -- TODO find an other issue
        candidates[i] = {
          ["name"] = candidate,
          ["status"] = nil,
        };
      else
        DEFAULT_CHAT_FRAME:AddMessage(format("FyLooter: WARNING %s isn't listed.", candidate), 1, 0, 0);
      end
    end
  end

  local data = FyLooter:Serialize(loot, candidates);

  FyLooter.Core:RequestRoll(data);
end

-- ========================================================================= --

--function FyLooter.Core:CheckVersion()
--  FyLooter:Debug("CheckVersion()");
--
--  local data = FyLooter:Serialize(FyLooter.Version);
--
--  FyLooter:SendCommMessage("FLer_VersionCheck", data, "GUILD");
--
--  if (GetNumRaidMembers() > 0) then
--    FyLooter:SendCommMessage("FLer_VersionCheck", data, "RAID");
--  end
--end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:CommitPlayers(target)  
  if (target) then
    FyLooter:Debug("CommitPlayers("..target..")");

    local data = FyLooter:Serialize(FyLooter.Data.timestamp, FyLooter.Data.players);

    FyLooter:SendCommMessage("FLer_PlayersCommit", data, "WHISPER", target);
  else
    if (FyLooter.Core.uncommitedChanges) then
      FyLooter:Debug("CommitPlayers()");

      FyLooter.Data.timestamp = time();
      
      local data = FyLooter:Serialize(FyLooter.Data.timestamp, FyLooter.Data.players);

      FyLooter:SendCommMessage("FLer_PlayersCommit", data, "GUILD");

      if (GetNumRaidMembers() > 0) then
        FyLooter:SendCommMessage("FLer_PlayersCommit", data, "RAID");
      end

      FyLooter.Core.uncommiterChanges = false;
    end
  end
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:CheckoutPlayers()
  FyLooter:Debug("CheckoutPlayers()");

  local data = FyLooter:Serialize(FyLooter.Data.timestamp, FyLooter.Data.players);

  FyLooter:SendCommMessage("FLer_PlayersCheckout", data, "GUILD");

  if (GetNumRaidMembers() > 0) then
    FyLooter:SendCommMessage("FLer_PlayersCheckout", data, "RAID");
  end  
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:RequestRoll(data)
  FyLooter:Debug("RequestRoll()");

  FyLooter:SendCommMessage("FLer_RollRequest", data, "RAID");
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:AnswerRoll(status)
  FyLooter:Debug("AnswerRoll()");

  local data = FyLooter:Serialize(FyLooter.Gui.Loot, status);

  FyLooter:SendCommMessage("FLer_RollAnswer", data, "RAID");
end

-- ========================================================================= --

--function FyLooter.Core:OnVersionCheck(data, distribution, sender)
--  FyLooter:Debug("OnVersionCheck("..sender..")");
--
--  if (sender == UnitName("player")) then return end
--
--  local ok, version = FyLooter:Deserialize(data);
--
--  if (ok) then
--    FyLooter:Debug(format("%s v%d.%d.%d", sender, version.Major, version.Minor, version.Patch));
--
--    if ((((FyLooter.Version.Major * 10000) + FyLooter.Version.Minor) * 10000 + FyLooter.Version.Patch) > (((version.Major * 10000) + version.Minor) * 10000 + version.Patch)) then
--      FyLooter:Debug("FLer_NewVersion send to "..sender);
--
--      FyLooter:SendCommMessage("FLer_NewVersion", format("%d.%d.%d", FyLooter.Version.Major, FyLooter.Version.Minor, FyLooter.Version.Patch), sender);
--    end
--  else
--    DEFAULT_CHAT_FRAME:AddMessage("FyLooter: " .. version, 1, 0, 0);
--  end
--end

-- ------------------------------------------------------------------------- --

--function FyLooter.Core:OnNewVersion(data, distribution, sender)
--  FyLooter:Debug("OnNewVersion("..sender..")");
--
--  DEFAULT_CHAT_FRAME:AddMessage("FyLooter: Une nouvelle version est disponible, veuillez mettre à jour votre version.", 1, 0.5, 0);
--end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:OnPlayersCommit(data, distribution, sender)
  FyLooter:Debug("OnPlayersCommit("..sender..")");

  if (sender == UnitName("player")) then return end

  local ok, timestamp, players = FyLooter:Deserialize(data);

  if (ok) then
    if (timestamp > FyLooter.Data.timestamp) then
      FyLooter:Delete(FyLooter.Data.players);
      FyLooter.Data.players = players;

      FyLooter.Data.timestamp = timestamp;

      DEFAULT_CHAT_FRAME:AddMessage(format("FyLooter: Players list updated from %s.", sender), 0.2, 0.5, 0.8);
      
      FyLooter.Model.Characters:Update();
      FyLooter.Gui:Update();
    end
  else
    DEFAULT_CHAT_FRAME:AddMessage("FyLooter: " .. timestamp, 1, 0, 0);
  end
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:OnPlayersCheckout(data, distribution, sender)
  FyLooter:Debug("OnPlayersCheckout("..sender..")");

  if (sender == UnitName("player")) then return end
  
  local ok, timestamp, players = FyLooter:Deserialize(data);

  if (ok) then
    if (timestamp < FyLooter.Data.timestamp) then
      FyLooter.Core:CommitPlayers(sender);
    elseif (timestamp > FyLooter.Data.timestamp) then
      FyLooter:Delete(FyLooter.Data.players);
      FyLooter.Data.players = players;

      FyLooter.Data.timestamp = timestamp;

      DEFAULT_CHAT_FRAME:AddMessage(format("FyLooter: Players list updated from %s.", sender), 0.2, 0.5, 0.8);
      
      FyLooter.Model.Characters:Update();
      FyLooter.Gui:Update();
    end
  else
    DEFAULT_CHAT_FRAME:AddMessage("FyLooter: " .. timestamp, 1, 0, 0);
  end
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:OnRollRequest(data, distribution, sender)
  FyLooter:Debug("OnRollRequest("..sender..")");

  local ok, loot, candidates = FyLooter:Deserialize(data);

  if (ok) then
    FyLooter:Delete(FyLooter.Data.candidates);
    FyLooter.Data.candidates = candidates;

    FyLooter.Gui:SetLoot(loot);
  else
    DEFAULT_CHAT_FRAME:AddMessage("FyLooter: " .. loot, 1, 0, 0);
  end
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:OnRollAnswer(data, distribution, sender)
  FyLooter:Debug("OnRollAnswer("..sender..")");

  local ok, loot, status = FyLooter:Deserialize(data);

  if (ok) then
    if (loot.item == FyLooter.Gui.Loot.item) then
      FyLooter.Model:SetStatus(sender, status);
    end
  else
    DEFAULT_CHAT_FRAME:AddMessage("FyLooter: " .. loot, 1, 0, 0);
  end
end

-- ------------------------------------------------------------------------- --

function FyLooter.Core:OnLootCleared(data, distribution, sender)
  FyLooter:Debug("OnLootCleared("..sender..")");

  if (data == FyLooter.Gui.Loot.item) then
    FyLooter:Delete(FyLooter.Data.candidates);

    FyLooter.Gui.Model:SetSelection(nil);
    FyLooter.Gui.FreeButton:Disable();
    FyLooter.Gui.PayButton:Disable();

    FyLooter.Gui.MainFrame:Hide();
    FyLooter.Gui.MinimapButton:Enable();
  end
end

-- ========================================================================= --

